package com.uchengwang.ui.cm_ui.umsg.ui;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.Editable;
import android.text.InputFilter;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.uchengwang.R;
import com.uchengwang.functions.http.config.BaseConfig;
import com.uchengwang.ui.base_ui.ActivityTitleBar;
import com.uchengwang.ui.base_ui.ui.AllBaseActivity;
import com.uchengwang.ui.utils.UCommonUtil;

import de.greenrobot.event.EventBus;

/**
 * Created by U城网 on 2017/12/15.
 * 第一次进入编辑享聊昵称
 */

public class WantChatNameActivity extends AllBaseActivity implements View.OnClickListener{

    private ActivityTitleBar titleBa;
    private EditText editor;
    private TextView num, editor_dec;
    private int length;
    private Button editor_bt;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_editor_want_chat);
        EventBus.getDefault().register(this);
        initView();

    }
    private void initView(){
        titleBa = (ActivityTitleBar) findViewById(R.id.want_editor_titleBa);
        editor = (EditText) findViewById(R.id.want_editor_editor);
        editor.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }
            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
                if (!TextUtils.isEmpty(charSequence)){
                    num.setText(charSequence.length()+"/"+length);
                }
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });
        num = (TextView) findViewById(R.id.want_editor_num);
        editor_dec = (TextView) findViewById(R.id.want_editor_dec);
        editor_bt = (Button) findViewById(R.id.want_editor_bt);
        editor_bt.setOnClickListener(this);
        editor_dec.setText("为自己起个个性的名字吧");

        initViewData();
    }

    private void initViewData(){
        titleBa.setTitle("享聊昵称", 0);
        editor.setFilters(new InputFilter[]{new InputFilter.LengthFilter(15)});
        num.setText("0/15");
        length = 15;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.want_editor_bt:
                String content = editor.getText().toString();
                content = content.trim();
                if (TextUtils.isEmpty(content)){
                    UCommonUtil.getUtil().showToast(this, "请输入享聊昵称");
                    return;
                }

                Intent intent = new Intent(this, WantChatEditorImgActivity.class);
                intent.putExtra(BaseConfig.TYPE, 0);
                intent.putExtra(BaseConfig.NICK_NAME, content);
                startActivity(intent);
                break;
        }
    }

    /**
     *首次进入 完成进入享聊 把注册享聊流程的界面finish掉
     * true 完成享聊的认证
     * */
    public void onEvent( Boolean b){
        if (b != null && b){
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
